from core.lib import NowStr
from factory import db

class BaseModel(db.Model):
    __abstract__ = True
    def to_dict(self):
        """
        将 ORM 实例转换为字典。
        如果实例中某项不存在（值为 None 或未设置），则该项的值为 None。
        确保所有字段的键都存在。
        """
        return {column.name: getattr(self, column.name) for column in self.__table__.columns}




class CidMixin:
    cid = db.Column(db.Integer)

class PidMixin:
    pid = db.Column(db.Integer)

class ActorMixin:
    uid = db.Column(db.Integer, nullable=False)
    time = db.Column(db.String(20), nullable=False, default=NowStr())

class AcceptorMixin:
    uid1 = db.Column(db.Integer)

class TitleMixin:
    title = db.Column(db.String(300))

class TextMixin:
    text = db.Column(db.Text)

class StatusMixin:
    published = db.Column(db.Boolean, default=False)
    checked = db.Column(db.Boolean, default=False)
    passed = db.Column(db.Boolean, default=False)

class SortMixin:
    sort = db.Column(db.Integer)

class AccountMixin:
    username = db.Column(db.String(300), nullable=False, unique=True)
    password = db.Column(db.String(300), nullable=False)

class ImageMixin:
    image = db.Column(db.Text)

class Image2Mixin:
    image1 = db.Column(db.Text)
    image2 = db.Column(db.Text)

class Images9Mixin:
    image1 = db.Column(db.Text)
    image2 = db.Column(db.Text)
    image3 = db.Column(db.Text)
    image4 = db.Column(db.Text)
    image5 = db.Column(db.Text)
    image6 = db.Column(db.Text)
    image7 = db.Column(db.Text)
    image8 = db.Column(db.Text)
    image9 = db.Column(db.Text)

# class LikePost(Base, ActorMixin, PidMixin):
#     pass
#
# class LikeComment(Base, ActorMixin, CidMixin):
#     pass
#
# class LikeUser(Base, ActorMixin, AcceptorMixin):
#     pass
#
# class Comment(Base, ActorMixin, PidMixin, TextMixin, StatusMixin):
#     pass
#
# class Post(Base, ActorMixin, TitleMixin, TextMixin, StatusMixin, SortMixin):
#     pass
#
# class Prove(Base, ActorMixin, StatusMixin, SortMixin):
#     pass
#


def insert(obj):
    try:
        db.session.add(obj)
        db.session.commit()
        return True  # 插入成
    except Exception as e:
        db.session.rollback()
        return False



